package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Println(mincostTickets([]int{6,9,10,14,15,16,17,18,20,22,23,24,29,30,31,33,35,37,38,40,41,46,47,51,54,57,59,65,70,76,77,81,85,87,90,91,93,94,95,97,98,100,103,104,105,106,107,111,112,113,114,116,117,118,120,124,128,129,135,137,139,145,146,151,152,153,157,165,166,173,174,179,181,182,185,187,188,190,191,192,195,196,204,205,206,208,210,214,218,219,221,225,229,231,233,235,239,240,245,247,249,251,252,258,261,263,268,270,273,274,275,276,280,283,285,286,288,289,290,291,292,293,296,298,299,301,303,307,313,314,319,323,325,327,329,334,339,340,341,342,344,346,349,352,354,355,356,357,358,359,363,364}, []int{21,115,345}))
}

func mincostTickets(days []int, costs []int) int {
	var lastAllDaysCost [366]int
	dayIdx := 0

	ticketDay, ticketWeek, ticketMonth := costs[0], costs[1], costs[2]
	lastAllDaysCost[0] = 0

	for today := 1; today <= 365; today ++ {
		if dayIdx >= len(days) {
			break
		}

		if days[dayIdx] != today {
			lastAllDaysCost[today] = lastAllDaysCost[today - 1]
			continue
		}

		dayIdx ++
		lastAllDaysCost[today] = min(lastAllDaysCost[max(0, today - 1)] + ticketDay,lastAllDaysCost[max(0, today - 7)] + ticketWeek,lastAllDaysCost[max(0, today - 30)] + ticketMonth)
	}
	return lastAllDaysCost[days[len(days) - 1]]
}

func min(nums ...int) int {
	a := math.MaxInt32
	for _, v := range nums {
		if v < a {
			a = v
		}
	}
	return a
}

func max (a, b int) int {
	if a > b {
		return a
	}
	return b
}
